// pages/core/home/home.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 0,
    bannerList: [],
    categories: [],
    activities: [],
    spots: [],
    weatherData: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.loadHomeData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.loadHomeData(() => {
      wx.stopPullDownRefresh();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 加载首页数据
  loadHomeData(callback) {
    wx.showLoading({
      title: '加载中...'
    });
    
    // 此处预留接口调用 - 获取首页数据
    const getHomePageData = () => {
      // 未来实现: 调用云函数获取首页数据
      // 目前使用模拟数据
      return new Promise((resolve) => {
        setTimeout(() => {
          resolve({
            code: 0,
            message: 'success',
            data: {
              banners: [
                {
                  id: 1,
                  image: '/assets/images/home/banner1.jpg',
                  title: '荔波小七孔 | 夏季避暑胜地',
                  linkType: 1,
                  linkUrl: ''
                },
                {
                  id: 2,
                  image: '/assets/images/home/banner2.jpg',
                  title: '平坝樱花园 | 春季赏花盛会',
                  linkType: 1,
                  linkUrl: ''
                },
                {
                  id: 3,
                  image: '/assets/images/home/banner3.jpg',
                  title: '黄果树瀑布 | 壮观自然奇景',
                  linkType: 1,
                  linkUrl: ''
                }
              ],
              categories: [
                {
                  id: 1,
                  name: '景点',
                  icon: '/assets/images/home/icon_map.png'
                },
                {
                  id: 2,
                  name: '购物',
                  icon: '/assets/images/home/icon_shop.png'
                },
                {
                  id: 3,
                  name: '康养',
                  icon: '/assets/images/home/icon_heart.png'
                },
                {
                  id: 4,
                  name: '票务',
                  icon: '/assets/images/home/icon_ticket.png'
                },
                {
                  id: 5,
                  name: '优惠',
                  icon: '/assets/images/home/icon_discount.png'
                }
              ],
              weatherData: {
                location: '贵阳市',
                weather: '晴',
                temperature: '26°C',
                highTemp: '28°C',
                healthIndices: {
                  oxygen: '92',
                  hiking: '适宜',
                  uv: '中等',
                  comfort: '舒适'
                }
              },
              activities: [
                {
                  id: 1,
                  title: '2025贵州民族文化节',
                  imageUrl: '/assets/images/home/activity1.jpg',
                  timeRange: '05/15-05/20',
                  price: 99
                },
                {
                  id: 2,
                  title: '贵州茶文化体验活动',
                  imageUrl: '/assets/images/home/activity2.jpg',
                  timeRange: '05/25-05/30',
                  price: 129
                },
                {
                  id: 3,
                  title: '侗族大歌表演',
                  imageUrl: '/assets/images/home/activity3.jpg',
                  timeRange: '06/01-06/05',
                  price: 88
                }
              ],
              spots: [
                {
                  id: 1,
                  title: '荔波小七孔',
                  imageUrl: '/assets/images/home/spot1.jpg',
                  tags: ['森林负氧', '情绪调节'],
                  rating: 4.9,
                  price: 138,
                  description: '拥有"地球腰带上的绿宝石"美誉，森林覆盖率高达97%，负氧离子含量极高。',
                  hasVideo: true,
                  hasPanorama: true
                },
                {
                  id: 2,
                  title: '平坝樱花园',
                  imageUrl: '/assets/images/home/spot2.jpg',
                  tags: ['舒缓身心', '缓解压力'],
                  rating: 4.8,
                  price: 69,
                  description: '亚洲最大的樱花园，拥有10万余株樱花树，春季盛开，漫天樱花如云似霞。',
                  hasVideo: true,
                  hasPanorama: false
                }
              ]
            }
          });
        }, 1000);
      });
    };
    
    getHomePageData()
      .then((res) => {
        if (res.code === 0) {
          const { banners, categories, activities, spots, weatherData } = res.data;
          
          // 处理轮播图数据
          const bannerList = banners.map((item) => ({
            value: item.id,
            image: item.image,
            title: item.title,
            type: item.linkType,
            url: item.linkUrl
          }));
          
          this.setData({
            bannerList,
            categories,
            activities,
            spots,
            weatherData
          });
        } else {
          wx.showToast({
            title: res.message || '获取数据失败',
            icon: 'none'
          });
        }
      })
      .catch((error) => {
        console.error('获取首页数据失败:', error);
        wx.showToast({
          title: '获取数据失败，请重试',
          icon: 'none'
        });
      })
      .finally(() => {
        wx.hideLoading();
        callback && callback();
      });
  },
  
  // 搜索点击事件
  onSearchTap() {
    wx.navigateTo({
      url: '/pages/service/search/search'
    });
  },
  
  // 轮播图点击事件
  onBannerClick(e) {
    const { index, item } = e.detail;
    
    // 根据linkType跳转到不同页面
    switch (item.type) {
      case 1: // 景点详情
        wx.navigateTo({
          url: `/pages/detail/scenic_detail/scenic_detail?id=${item.value}`
        });
        break;
      case 2: // 活动详情
        wx.navigateTo({
          url: `/pages/detail/activity_detail/activity_detail?id=${item.value}`
        });
        break;
      case 3: // 外部链接
        // 处理外部链接
        wx.showToast({
          title: '暂不支持外部链接',
          icon: 'none'
        });
        break;
      default:
        break;
    }
  },
  
  // 分类点击事件
  onCategoryTap(e) {
    const category = e.currentTarget.dataset.category;
    wx.navigateTo({
      url: `/pages/service/category/category?id=${category.id}&name=${category.name}`
    });
  },
  
  // 更多点击事件
  onMoreTap(e) {
    const type = e.currentTarget.dataset.type;
    
    switch (type) {
      case 'spot':
        wx.switchTab({
          url: '/pages/core/discover/discover'
        });
        break;
      case 'activity':
        wx.navigateTo({
          url: '/pages/service/activity/activity_list'
        });
        break;
      default:
        break;
    }
  },
  
  // 活动点击事件
  onActivityTap(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/detail/activity_detail/activity_detail?id=${id}`
    });
  },
  
  // 景点点击事件
  onSpotTap(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/detail/scenic_detail/scenic_detail?id=${id}`
    });
  },
  
  // 季节主题点击事件
  onSeasonTap(e) {
    const season = e.currentTarget.dataset.season;
    wx.navigateTo({
      url: `/pages/service/season/season?type=${season}`
    });
  },
  
  // 切换城市
  onLocationTap() {
    wx.showToast({
      title: '切换城市功能开发中',
      icon: 'none'
    });
  }
})